/*
 * (C) Copyright 2008-2011 Freescale Semiconductor, Inc.
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

/* #define DEBUG */

#include <common.h>

#include <command.h>
#include <environment.h>
#include <linux/stddef.h>
#include <malloc.h>
#include <mmc.h>
#include <search.h>
#include <errno.h>
#include <oem_inand.h>

/* references to names in env_common.c */
extern uchar default_environment[];
extern struct hsearch_data env_htab;
char *env_name_spec = "System Disk";

#ifdef ENV_IS_EMBEDDED
extern uchar environment[];
env_t *env_ptr = (env_t *)(&environment[0]);
#else /* ! ENV_IS_EMBEDDED */
env_t *env_ptr = NULL;
#endif /* ENV_IS_EMBEDDED */

/* local functions */
#if !defined(ENV_IS_EMBEDDED)
static void use_default(void);
#endif

DECLARE_GLOBAL_DATA_PTR;

#if !defined(CONFIG_ENV_OFFSET)
#define CONFIG_ENV_OFFSET 0
#endif

#if defined(CONFIG_SYS_DISK_MMC)
static int __mmc_get_env_addr(struct mmc *mmc, u32 *env_addr)
{
	/*printf("in __mmc_get_env_addr\n");*/
	*env_addr = (u32 *)CONFIG_ENV_OFFSET;
	return 0;
}
__attribute__((weak, alias("__mmc_get_env_addr")))
int mmc_get_env_addr(struct mmc *mmc, u32 *env_addr);

int init_mmc_for_env(struct mmc *mmc)
{
	/*puts("in init_mmc_for_env\n");*/
	if (!mmc) {
		puts("No MMC card found\n");
		return -1;
	}

	if (mmc_init(mmc)) {
		puts("MMC init failed\n");
		return  -1;
	}

	return 0;
}
static int init_sysdisk_for_env(int *offset , int* write_blksz)
{
	struct mmc *mmc = find_mmc_device(SYS_DISK_CHANNEL);

	if (init_mmc_for_env(mmc))
		return 1;

	if(mmc_get_env_addr(mmc, (u32 *)offset))
		return 1;
	*write_blksz=mmc->write_bl_len;
	return 0;
}
#elif defined(CONFIG_SYS_DISK_USB)
static int initiated=0;
static int init_sysdisk_for_env(int *offset , int* write_blksz)
{
	block_dev_desc_t *dev=NULL;
	if(!initiated){
		usb_stop();
		if (usb_init()>= 0)   {
			if(usb_stor_scan(0)!=0){
				printf("no system disk found\n");
				return -1;
			}
		}
		initiated=1;
	}
	*offset=CONFIG_ENV_OFFSET;
	* write_blksz=BLOCK_SIZE;
	return 0;
}
#else
static int init_sysdisk_for_env(int *offset , int* write_blksz)
{
	return -1;
}
#endif
uchar env_get_char_spec(int index)
{
	/*printf("in env_get_char_spec\n");*/
	return *((uchar *)(gd->env_addr + index));
}

int env_init(void)
{
	gd->env_addr = (ulong)&default_environment[0];
	gd->env_valid = 1;

	return 0;
}
#ifdef CONFIG_CMD_SAVEENV

int saveenv(void)
{
	env_t	env_new;
	int offset , write_blksz;

	if(init_sysdisk_for_env(&offset , &write_blksz))return -1;
	env_new = *env_ptr; // Tye Added for put memory env to env_new
	env_new.crc   = crc32(0, env_new.data, ENV_SIZE);
	printf("Writing enviroments to system disk... ");
	if(oem_write_sys_disk((uint8_t  *)(&env_new), offset,(int) CONFIG_ENV_SIZE ,(int) CONFIG_ENV_SIZE)){
		puts("failed\n");
		return 1;
	}
	return 0;
}
#endif /* CONFIG_CMD_SAVEENV */

void env_relocate_spec(void)
{
	char buf[CONFIG_ENV_SIZE];
	u32 offset , write_blksz;

	if(init_sysdisk_for_env(&offset , &write_blksz)){
		use_default();
		return ;
	}
	if(oem_read_sys_disk((u_char *)buf , offset , CONFIG_ENV_SIZE)){
		use_default();
		return;
	}
	env_import(buf, 1);
	memcpy(env_ptr,buf,CONFIG_ENV_SIZE);
}

static void use_default()
{
	/*puts("in use_default\n");*/
	set_default_env();
}
